package com.yanqu.road.dao.impl.activity.diggold;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.diggold.config.DigGoldAntiqueConfig;
import com.yanqu.road.entity.activity.diggold.config.DigGoldBlockConfig;
import com.yanqu.road.entity.activity.diggold.config.DigGoldMapConfig;
import com.yanqu.road.entity.activity.diggold.config.DigGoldStratumConfig;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DigGoldConfigDaoImpl extends TempDao {
    private static String getDigGoldAntiqueConfigSql = "select * from `t_s_activity_diggold_antique` where `activityId`=?";
    public Map<Integer,DigGoldAntiqueConfig> getDigGoldAntiqueConfigMap(int activityId) {
        Map<Integer,DigGoldAntiqueConfig> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getDigGoldAntiqueConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                DigGoldAntiqueConfig digGoldAntiqueConfig = new DigGoldAntiqueConfig();
                digGoldAntiqueConfig.setActivityId(rs.getInt("activityId"));
                digGoldAntiqueConfig.setAntiqueId(rs.getInt("antiqueId"));
                digGoldAntiqueConfig.setIcon(rs.getInt("icon"));
                digGoldAntiqueConfig.setType(rs.getInt("type"));
                digGoldAntiqueConfig.setName(rs.getString("name"));
                digGoldAntiqueConfig.setDesc(rs.getString("desc"));
                digGoldAntiqueConfig.setValue(rs.getInt("value"));
                digGoldAntiqueConfig.setWeight(rs.getInt("weight"));
                map.put(digGoldAntiqueConfig.getAntiqueId(),digGoldAntiqueConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    private static String getDigGoldBlockConfigSql = "select * from `t_s_activity_diggold_block` where `activityId`=?";
    public Map<Integer,DigGoldBlockConfig> getDigGoldBlockConfigMap(int activityId) {
        Map<Integer,DigGoldBlockConfig> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getDigGoldBlockConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                DigGoldBlockConfig digGoldBlockConfig = new DigGoldBlockConfig();
                digGoldBlockConfig.setActivityId(rs.getInt("activityId"));
                digGoldBlockConfig.setType(rs.getInt("type"));
                digGoldBlockConfig.setName(rs.getString("name"));
                digGoldBlockConfig.setDesc(rs.getString("desc"));
                digGoldBlockConfig.setCostHp(rs.getInt("costHp"));
                map.put(digGoldBlockConfig.getType(),digGoldBlockConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    private static String getDigGoldMapConfigSql = "select * from `t_s_activity_diggold_map` where `activityId`=?";
    public Map<Integer,DigGoldMapConfig> getDigGoldMapConfigMap(int activityId) {
        Map<Integer,DigGoldMapConfig> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getDigGoldMapConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                DigGoldMapConfig digGoldMapConfig = new DigGoldMapConfig();
                digGoldMapConfig.setActivityId(rs.getInt("activityId"));
                digGoldMapConfig.setId(rs.getInt("id"));
                digGoldMapConfig.setType(rs.getInt("type"));
                digGoldMapConfig.setDynamiteWeight(rs.getString("dynamiteWeight"));
                digGoldMapConfig.setDrillWeight(rs.getString("drillWeight"));
                digGoldMapConfig.setShovelWeight(rs.getString("shovelWeight"));
                digGoldMapConfig.setAntiqueWeight(rs.getString("antiqueWeight"));
                digGoldMapConfig.setParam(rs.getString("param"));
                digGoldMapConfig.setWeight(rs.getInt("weight"));
                map.put(digGoldMapConfig.getId(),digGoldMapConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    private static String getDigGoldStratumConfigSql = "select * from `t_s_activity_diggold_stratum` where `activityId`=?";
    public Map<Integer,DigGoldStratumConfig> getDigGoldStratumConfigMap(int activityId) {
        Map<Integer,DigGoldStratumConfig> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getDigGoldStratumConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                DigGoldStratumConfig digGoldStratumConfig = new DigGoldStratumConfig();
                digGoldStratumConfig.setActivityId(rs.getInt("activityId"));
                digGoldStratumConfig.setId(rs.getInt("id"));
                digGoldStratumConfig.setName(rs.getString("name"));
                digGoldStratumConfig.setUnlock(rs.getInt("unlock"));
                digGoldStratumConfig.setMagnification(rs.getInt("magnification"));
                digGoldStratumConfig.setReward(rs.getString("reward"));

                List<String> strings = StringUtils.stringToStringList(digGoldStratumConfig.getReward(), "&");
                int index = 0;
                Map<Integer,String> rewardMap = new ConcurrentHashMap<>();
                for (String string : strings) {
                    rewardMap.put(index,string);
                    index++;
                }
                digGoldStratumConfig.setRewardMap(rewardMap);

                digGoldStratumConfig.setMapRate(rs.getString("mapRate"));

                Map<Integer,Integer> mapRateMap = new ConcurrentHashMap<>();
                List<String> rateList = StringUtils.stringToStringList(digGoldStratumConfig.getMapRate(), ";");
                for (String string : rateList) {
                    List<Integer> rate = StringUtils.stringToIntegerList(string, ",");
                    mapRateMap.put(rate.get(0),rate.get(1));
                }
                digGoldStratumConfig.setMapRateMap(mapRateMap);

                map.put(digGoldStratumConfig.getId(),digGoldStratumConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }




}
